package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DevelopJudgeFormMap;
import com.iemes.entity.DevelopProgressFormMap;
import com.iemes.entity.LoadDailyReportFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.SurfaceCheckFormMap;
import com.iemes.entity.SurfaceCheckStandardFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.DevelopJudgeMapper;
import com.iemes.mapper.mould.DevelopProgressMapper;
import com.iemes.mapper.mould.SurfaceCheckStandardMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/developJudge/")
@Controller
public class DevelopJudgeController extends BaseController {
	
	@Inject
	private DevelopJudgeMapper developJudgeMapper;
	
	@Inject
	private DevelopProgressMapper developProgressMapper;
	
	@Inject
	private SurfaceCheckStandardMapper surfaceCheckStandardMapper;

	@RequestMapping("developJudge_list")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/developJudge/developJudge_list";
	}
	
	@ResponseBody
	@RequestMapping("developJudge_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		DevelopJudgeFormMap developJudgeFormMap = getFormMap(DevelopJudgeFormMap.class);
		List<Map<String, Object>> developJudge = developJudgeMapper.getAll(developJudgeFormMap);
		LuogicPaging(pageNow, pageSize, developJudge);
		return pageView;
	}
	
	@RequestMapping("developJudge_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询模具开发/变更评审表
			DevelopJudgeFormMap judge=developJudgeMapper.getById(id);
			model.addAttribute("judge",judge);
		}
		return Common.BACKGROUND_PATH + "/mould/developJudge/developJudge_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="developJudge_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="新开模开发申请",methods="模具开发/变更评审表-编辑模具开发/变更评审表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		DevelopJudgeFormMap developJudgeFormMap = getFormMap(DevelopJudgeFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		developJudgeFormMap.put("date", sdf.format(date));
		developJudgeFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		developJudgeFormMap.put("status", 1);
		baseMapper.editEntity(developJudgeFormMap);
		//添加模具开发进度跟踪表
		DevelopProgressFormMap developProgressFormMap=new DevelopProgressFormMap();
		String developjudgeId=developJudgeFormMap.getStr("id");
		DevelopProgressFormMap progress=developProgressMapper.getByDevelopjudgeId(developjudgeId);
		if(progress==null || progress.size()<=0){
			developProgressFormMap.put("developjudgeId", developJudgeFormMap.get("id"));
			baseMapper.addEntity(developProgressFormMap);
			
			//循环添加模具制造外观质量验收单检查标准
			SurfaceCheckStandardFormMap surfaceCheckStandardFormMap=new SurfaceCheckStandardFormMap();
			List<Map<String, Object>> getAll=surfaceCheckStandardMapper.getAll(surfaceCheckStandardFormMap);
			for(Map<String, Object> map:getAll){
				SurfaceCheckFormMap surfaceCheckFormMap=new SurfaceCheckFormMap();
			//	surfaceCheckFormMap.put("", value);
			}
			
//			MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
//			mouldCheckStandardFormMap.put("status", 1);
//			List<Map<String, Object>> getAllItemBy2=mouldCheckStandardMapper.getAllItemBy2(mouldCheckStandardFormMap);
//			for(Map<String, Object> map:getAllItemBy2){
//				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
//				mouldUnpassHandleFormMap.put("standardId",map.get("id"));
//				mouldUnpassHandleFormMap.put("checkId", pAfterCheckFormMap.get("id"));
//				mouldUnpassHandleFormMap.put("repairPeopleId",11);
//				mouldUnpassHandleFormMap.put("supervisorId",11);
//				baseMapper.addEntity(mouldUnpassHandleFormMap);
//			}
		}
		return "success";
	}
}












